import sqlite3

file = open("output.txt","w",encoding='utf-8')
file.write("local p={}\n")
category = 6
con = sqlite3.connect("master.mdb")
cur = con.cursor()
def sql2lua(table_name,category=None):
    # 首先统计表名
    name_array = []
    cur.execute("PRAGMA table_info([" + table_name + "])")
    for x in cur:
        name_array.append(x[1])
    print(name_array)
    # 读取表内容
    if category==None:
        file.write("p."+table_name+"={\n")
        cur.execute("select * from " + table_name)
    else:
        file.write("p." + table_name + "_" + str(category) +"={\n")
        cur.execute("select * from "+table_name+" where category = %d" %category)

    is_first = True
    for x in cur:
        if is_first:
            is_first = False
        else:
            file.write(",\n")
        text_is_first = True
        file.write("{")
        for i in range(len(name_array)):
            if text_is_first:
                text_is_first = False
            else:
                file.write(",")
            file.write(name_array[i] + "=\"" + str(x[i]) + "\"")
        file.write("}")
    file.write("\n}\n")

# 导出chara_data
sql2lua("chara_data")
# 导出skill_data
sql2lua("skill_data")
# 导出available_skill_set
sql2lua("available_skill_set")
# 导出text_data_29
sql2lua("text_data",29)
# 导出text_data_47
sql2lua("text_data",47)
# 导出single_mode_route_race
sql2lua("single_mode_route_race")
# 导出single_mode_program
sql2lua("single_mode_program")
# 导出single_mode_race_group
sql2lua("single_mode_race_group")
# 导出single_mode_reward_set
sql2lua("single_mode_reward_set")
# 导出single_mode_skill_need_point
sql2lua("single_mode_skill_need_point")
# 导出single_mode_scout_chara
sql2lua("single_mode_scout_chara")
# 导出card_talent_upgrade
sql2lua("card_talent_upgrade")
# 导出card_rarity_data
sql2lua("card_rarity_data")
# 导出card_data
sql2lua("card_data")


file.write("\nreturn p")